package com.cloudfun.campusshare.common.model.dto.dingtalk;

import com.alibaba.fastjson.JSONObject;
import com.cloudfun.campusshare.common.constant.DingTalkRobotMsgType;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.stream.Collectors;

/**
 * Created by Huqin on 2020/5/13
 */
@Data
public class RobotMessageDTO {

    private final Content content;

    private RobotMessageAtDTO at;

    public RobotMessageDTO(Content content) {
        this.content = content;
    }

    public RobotMessageDTO(Content content, RobotMessageAtDTO at) {
        this.content = content;
        this.at = at;
        if (CollectionUtils.isNotEmpty(at.getAtMobiles())) {
            String atStr = StringUtils.join(at.getAtMobiles().stream().map(s -> "@" + s).collect(Collectors.toList()), ", ");
            this.content.appendContent(" " + atStr);
        }
    }

    public String toJson() {
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(this);
        Object content = jsonObject.remove("content");
        DingTalkRobotMsgType mstType = this.getContent().getMsgType();
        jsonObject.put(mstType.name().toLowerCase(), content);
        jsonObject.put("msgtype", mstType.name().toLowerCase());
        return jsonObject.toJSONString();
    }

}
